package com.hazy.animalface.presenter;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.DeleteCallback;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.hazy.animalface.event.RefreshStickersEvent;
import com.hazy.animalface.module.Sticker;
import com.hazy.animalface.module.StickerBag;
import com.hazy.animalface.utils.ImageToByte;
import com.hazy.animalface.view.ILoadView;

import org.greenrobot.eventbus.EventBus;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by pengjiahui on 2017/4/15 0015.
 * <p/>
 * 下载的接口逻辑实现
 */
public class ILoadPresenterCompl implements ILoadPresenter {

    private ILoadView iLoadView;

    public ILoadPresenterCompl(ILoadView iLoadView) {
        this.iLoadView = iLoadView;
    }

    @Override
    public void onLoad(int page) {

        AVQuery<StickerBag> stickerBagAVQuery = AVQuery.getQuery(StickerBag.class);
        stickerBagAVQuery.whereLessThan("id", page * 20);
        stickerBagAVQuery.whereGreaterThanOrEqualTo("id", (page - 1) * 20);
        stickerBagAVQuery.findInBackground(new FindCallback<StickerBag>() {
            @Override
            public void done(List<StickerBag> list, AVException e) {
                iLoadView.onLoadResult(list);
            }
        });


//        String cql = "select * from StickerBag limit " + (page - 1) * 20 + " , " + page * 20;
//        AVQuery.doCloudQueryInBackground(cql, new CloudQueryCallback<AVCloudQueryResult>() {
//            @Override
//            public void done(AVCloudQueryResult avCloudQueryResult, AVException e) {
//                List<StickerBag> results = (List<StickerBag>) avCloudQueryResult.getResults();
//                iDownloadView.onLoadResult(results);
//            }
//        }, StickerBag.class);
    }

    @Override
    public void loadSticiersByName(String stickerName) {
        AVQuery<Sticker> stickerAVQuery = AVQuery.getQuery(Sticker.class);
        stickerAVQuery.whereEqualTo(Sticker.PARENTNAME,stickerName);
        stickerAVQuery.findInBackground(new FindCallback<Sticker>() {
            @Override
            public void done(List<Sticker> list, AVException e) {
                iLoadView.onLoadSrickerDetail(true,list);
            }
        });
    }

    @Override
    public void removeStickerBag(String stickerName) {
        AVQuery<Sticker> stickerAVQuery = AVQuery.getQuery(Sticker.class);
        stickerAVQuery.whereEqualTo(Sticker.PARENTNAME,stickerName);
        stickerAVQuery.deleteAllInBackground(new DeleteCallback() {
            @Override
            public void done(AVException e) {
                EventBus.getDefault().post(new RefreshStickersEvent());
            }
        });
    }

    @Override
    public void doDownload(String name) {

    }

    @Override
    public void createStickerBag(String name, String price, AVFile thumb) {

        AVObject mStickerBag = new AVObject("StickerBag");
        mStickerBag.put("name", name);
        mStickerBag.put("parentName", name);
        mStickerBag.put("price", price);
        mStickerBag.put("thumb", thumb);
        mStickerBag.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    iLoadView.onCreateResult(true, "");
                } else {
                    iLoadView.onCreateResult(false, e.getMessage());
                }
            }
        });
    }

    int progress = 0;

    @Override
    public void uploadStickers(String name, ArrayList<String> paths) {
        for (int i = 0; i < paths.size(); i++) {
            try {
                AVObject mSticker = new AVObject("Sticker");
                mSticker.put("parent", name);
                mSticker.put("name", name + i);
                mSticker.put("image", new AVFile(name + i, ImageToByte.getBytes(new FileInputStream(paths.get(i)))));
                mSticker.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e == null) {
                            progress++;
                            iLoadView.onUploadResult(true, progress, "");
                        }
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void setProgressBarVisiblity(int visiblity) {

    }
}
